<template>
  <j-modal
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    @ok="handleOk"
    width="40%"
    :confirmLoading="confirmLoading"
    :okText="$t('保存')"
    :cancelText="$t('关闭')"
    v-drag
  >
    <div>
      <a-form-model :rules="rules" :model="dataForm" ref="form" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item prop="code" ref="code" :label="$t('激活码')">
          <a-input v-model="dataForm.code"></a-input>
        </a-form-model-item>
      </a-form-model>
    </div>
  </j-modal>
</template>

<script>
import { postAction, getAction } from '@/api/manage'
export default {
  name: 'ShouqbModal',
  components: {},
  data() {
    return {
      model: {},
      title: '',
      visible: false,
      confirmLoading: false,
      dataForm: {
        code: '',
      },
      labelCol: {
        xs: { span: 20 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 18 },
      },
      rules: {
        code: [{ required: true, message: this.$t('请输入激活码') }],
      },
      url: {
        add: '/pay/bisPayChannelShouqb/add',
        edit: '/pay/bisPayChannelShouqb/edit',
      },
    }
  },
  mounted() {},
  methods: {
    edit() {
      let that = this
      that.visible = true
      getAction('/pay/bisPayChannelShouqb/list').then((res) => {
        if (res.success) {
          if (res.result.records.length != 0) {
            that.dataForm = Object.assign({}, res.result.records[0])
          } else {
            that.dataForm = {
              code: '',
            }
          }
        }
      })
    },
    handleOk() {
      let that = this
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let url = ''
          if (that.dataForm.id) {
            url = that.url.edit
          } else {
            url = that.url.add
          }
          that.dataForm.code = that.dataForm.code ? that.dataForm.code.trim() : that.dataForm.code
          postAction(url, that.dataForm)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
</style>